package com.slicktechnologies.shared.common.helperlayer;

import com.google.gwt.core.shared.GwtIncompatible;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.annotation.OnSave;
import com.simplesoftwares.client.library.appstructure.SuperModel;
import com.simplesoftwares.client.library.appstructure.search.Filter;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.client.library.helperconstants.FormTypes;
import com.simplesoftwares.rebind.annatonations.TableAnnotation;
import com.slicktechnologies.client.config.configurations.ConfigTypes;
import com.slicktechnologies.shared.MyUtility;
import com.slicktechnologies.shared.common.relationalLayer.BranchRelation;
import com.slicktechnologies.shared.common.relationalLayer.EmployeeRelation;

// TODO: Auto-generated Javadoc
/**
 * Represents software User for login purpose. At present
 *  corresponding to 1 employee there is only 1 user.
 */
@Entity
public class User extends SuperModel {
	
	/***********************************************Entity Attributes****************************************************/
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -888074958627156271L;
	
	/** The user name. */
	@Index protected String userName;
	
	/** The acess level granted to user */
	protected String acessLevel;
	
	/** The employee name for this user as all user are employees */
	@Index
	protected String employeeName;
	
	/** The password. */
	@Index protected String password;
    
    /** The status. */
    protected boolean status;
    
    protected String branch;
    
   
    /********************************************************Relational Attributes ********************************************/
    protected Key<EmployeeRelation>keyEmployee;
    protected Key<Config>acessLevelKey;
    protected Key<BranchRelation> branchKey;
    
    
	
    /***********************************************Default Ctor****************************************************/
	/**
	 * Instantiates a new user.
	 */
	public User() {
		super();
		userName="";
		acessLevel="";
		employeeName="";
		password="";
		branch="";
		
		
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(SuperModel arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.slicktechnologies.shared.common.helperlayer.SuperModel#isDuplicate(com.slicktechnologies.shared.common.helperlayer.SuperModel)
	 */
	

	/***********************************************Getter/Setter****************************************************/
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 2, isFieldUpdater = false, isSortable = true, title = "User ID")
	public String getUserName() {
		return userName;
	}

	/**
	 * Sets the user name.
	 *
	 * @param userName the new user name
	 */
	public void setUserName(String userName) {
		if(userName!=null)
		  this.userName = userName.trim();
	}

	/**
	 * Gets the acess level.
	 *
	 * @return the acess level
	 */
	public String getAccessLevel() {
		return acessLevel;
	}

	/**
	 * Sets the acess level.
	 *
	 * @param acessLevel the new acess level
	 */
	public void setAccessLevel(String acessLevel) {
	    if(acessLevel!=null)
		  this.acessLevel = acessLevel.trim();
	}

	/**
	 * Gets the employee name.
	 *
	 * @return the employee name
	 */
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 1, isFieldUpdater = false, isSortable = true, title = "Employee")
	public String getEmployeeName() {
		return employeeName;
	}

	/**
	 * Sets the employee name.
	 *
	 * @param employeeName the new employee name
	 */
	public void setEmployeeName(String employeeName) {
		if(employeeName!=null)
		  this.employeeName = employeeName.trim();
	}

	/**
	 * Gets the password.
	 *
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	
	

	/**
	 * Sets the password.
	 *
	 * @param password the new password
	 */
	public void setPassword(String password) {
		if(password!=null)
		  this.password = password.trim();
	}
	
	@Override
	public boolean isDuplicate(SuperModel m) {
			
			boolean res1=isDuplicateUser(m);
			boolean res2=isDuplicateEmployee(m);
			System.out.println("Result of User Duplicate  "+(res1));
			System.out.println("Result of Employee Duplicate  "+(res2));
			
			return res1||res2;
			
	    }

	public boolean isDuplicateUser(SuperModel model)
	{
		User entity = (User) model;
		String name = entity.getUserName().trim();
		name=name.replaceAll("\\s","");
		name=name.toLowerCase();
		String curname=this.getUserName().trim();
		curname=curname.replaceAll("\\s","");
		curname=curname.toLowerCase();
		//New Object is being added
		if(id==null)
		{
			if(name.equals(curname))
				return true;
			else
				return false;
		}
		
		// Old object is being updated
		else
		{
			
			if(id.equals(entity.id))
			{
			  return false;	
			}
			if((name.equals(curname)==true))
			  return true;
			else
				return false;
				
		}
	}

	public boolean isDuplicateEmployee(SuperModel model)
	{
		User entity = (User) model;
		String name = entity.getEmployeeName().trim();
		String curname=this.employeeName.trim();
		curname=curname.replaceAll("\\s","");
		curname=curname.toLowerCase();
		name=name.replaceAll("\\s","");
		name=name.toLowerCase();
		
		//New Object is being added
		if(id==null)
		{
			if(name.equals(curname))
				return true;
			else
				return false;
		}
		
		// Old object is being updated
		else
		{
			
			if(id.equals(entity.id))
			{
			  return false;	
			}
			if((name.equals(curname)==true))
			  return true;
			else
				return false;
				
		}
	}


	/**
	 * Checks if is status.
	 *
	 * @return true, if is status
	 */
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 4, isFieldUpdater = false, isSortable = true, title = "Status")
	public Boolean getStatus() {
		return status;
	}

	/**
	 * Sets the status.
	 *
	 * @param status the new status
	 */
	public void setStatus(boolean status) {
		this.status = status;
	}
	
	
	
	public String getAcessLevel() {
		return acessLevel;
	}

	public void setAcessLevel(String acessLevel) {
		this.acessLevel = acessLevel;
	}

	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 2, isFieldUpdater = false, isSortable = true, title = "Branch")
	public String getBranch() {
		return branch;
	}

	public void setBranch(String branch)
	{
		if(branch!=null)
			this.branch = branch.trim();
	}

	public Key<EmployeeRelation> getKeyEmployee() {
		return keyEmployee;
	}

	public void setKeyEmployee(Key<EmployeeRelation> keyEmployee) {
		this.keyEmployee = keyEmployee;
	}

	public Key<Config> getAcessLevelKey() {
		return acessLevelKey;
	}

	public void setAcessLevelKey(Key<Config> acessLevelKey) {
		this.acessLevelKey = acessLevelKey;
	}

	public Key<BranchRelation> getBranchKey() {
		return branchKey;
	}

	public void setBranchKey(Key<BranchRelation> branchKey) {
		this.branchKey = branchKey;
	}

	/******************************************************************************************************************************/
	 @GwtIncompatible
	  @OnSave
	  public void OnSave()
	  {
		 
		  
			  MyQuerry querry=new MyQuerry();
			  
			  Filter filter=new Filter();
			  filter.setQuerryString("employeeName");
			 
			  filter.setStringValue(this.employeeName);
			  filter.setQuerryString(this.branch);
			  System.out.println("Querry Filter "+querry.getFilters());
			  querry.getFilters().add(filter);
			  querry.setQuerryObject(new EmployeeRelation() );
			  keyEmployee=(Key<EmployeeRelation>) MyUtility.getRelationalKeyFromCondition(querry);	
			  
			  filter=new Filter();
			  filter.setQuerryString("branchName");
			  filter.setQuerryString(this.branch);
			  System.out.println("Querry Filter "+querry.getFilters());
			  querry.getFilters().add(filter);
			  querry.setQuerryObject(new BranchRelation() );
			  
			  
			  branchKey=(Key<BranchRelation>) MyUtility.getRelationalKeyFromCondition(querry);
			 
		  
			 
			
	  }
	  @GwtIncompatible
	  @OnLoad
	  public void OnLoad()
	  {
		  //Get Entity From Key for Branch 
		
		  if(keyEmployee!=null)
		  {
			  String employeeName=MyUtility.getEmployeeNameFromKey(keyEmployee);
			  if(employeeName!=null)
			  {
				  if(employeeName.equals("")==false)
					  setEmployeeName(employeeName.trim());
			  }
		  }
		  
		  if(acessLevelKey!=null)
		  {
			  String acesslevel=MyUtility.getConfigNameFromKey(acessLevelKey);
			  if(acesslevel!=null)
			  {
				  if(acesslevel.equals("")==false)
					  setAccessLevel(acesslevel.trim());
			  }
		  }
		  
		 if(branchKey!=null)
		  {
			  
			  String branchName=MyUtility.getBranchNameFromKey(branchKey);
			  if(branchName!=null)
			  {
				  if(branchName.equals("")==false)
					  setBranch(branchName);
			  }
		  }
	  }

}
